$(function () {
	vm.getEquipment();
});

var setting = {
	data: {
		simpleData: {
			enable: true,
			idKey: "id",
			pIdKey: "parentId",
			rootPId: -1
		},
		key: {
			url:"nourl"
		}
	},
	callback : {
		// 单击事件
		onClick : zTreeOnClick
	}
};
// 单击事件，向后台发起请求
function zTreeOnClick(event, treeId, treeNode) {
	if (treeNode.level == "3") {
		var form1 =layui.form;
		$.get(baseURL + "baseinfo/lhpthresholdsetting/getInfo/"+treeNode.id, function(r){
			if(r.lhpThresholdSetting!=null){
				vm.lhpThresholdSetting = r.lhpThresholdSetting;
				//能见度
				if(vm.lhpThresholdSetting.visibilityStatus=='0'){
					$("#visibilityStatus").prop("checked",true);
				}else {
					$("#visibilityStatus").prop("checked",false);
				}
				//路面温度
				if(vm.lhpThresholdSetting.roadSurfaceTempStatus=='0'){
					$("#roadSurfaceTempStatus").prop("checked",true);
				}else {
					$("#roadSurfaceTempStatus").prop("checked",false);
				}
				//摩擦系数
				if(vm.lhpThresholdSetting.frictionStatus=='0'){
					$("#frictionStatus").prop("checked",true);
				}else {
					$("#frictionStatus").prop("checked",false);
				}
				//水膜高度
				if(vm.lhpThresholdSetting.waterFilmHeightStatus=='0'){
					$("#waterFilmHeightStatus").prop("checked",true);
				}else {
					$("#waterFilmHeightStatus").prop("checked",false);
				}
				//路面状态
				if(vm.lhpThresholdSetting.roadConditionStatus=='0'){
					$("#roadConditionStatus").prop("checked",true);
				}else {
					$("#roadConditionStatus").prop("checked",false);
				}
				//风速
				if(vm.lhpThresholdSetting.windSpeedStatus=='0'){
					$("#windSpeedStatus").prop("checked",true);
				}else {
					$("#windSpeedStatus").prop("checked",false);
				}
				//含冰比例
				if(vm.lhpThresholdSetting.icePercentageStatus=='0'){
					$("#icePercentageStatus").prop("checked",true);
				}else {
					$("#icePercentageStatus").prop("checked",false);
				}
				//相对湿度
				if(vm.lhpThresholdSetting.relativeHumidityStatus=='0'){
					$("#relativeHumidityStatus").prop("checked",true);
				}else {
					$("#relativeHumidityStatus").prop("checked",false);
				}
				//降水强度
				if(vm.lhpThresholdSetting.precipitationIntensityStatus=='0'){
					$("#precipitationIntensityStatus").prop("checked",true);
				}else {
					$("#precipitationIntensityStatus").prop("checked",false);
				}
				//空气温度
				if(vm.lhpThresholdSetting.airTempStatus=='0'){
					$("#airTempStatus").prop("checked",true);
				}else {
					$("#airTempStatus").prop("checked",false);
				}
				//降水量
				if(vm.lhpThresholdSetting.precipitationStatus=='0'){
					$("#precipitationStatus").prop("checked",true);
				}else {
					$("#precipitationStatus").prop("checked",false);
				}
				//水积累量
				if(vm.lhpThresholdSetting.waterAmountStatus=='0'){
					$("#waterAmountStatus").prop("checked",true);
				}else {
					$("#waterAmountStatus").prop("checked",false);
				}
				//雪积累量
				if(vm.lhpThresholdSetting.snowAmountStatus=='0'){
					$("#snowAmountStatus").prop("checked",true);
				}else {
					$("#snowAmountStatus").prop("checked",false);
				}
				//冰积累量
				if(vm.lhpThresholdSetting.iceAmountStatus=='0'){
					$("#iceAmountStatus").prop("checked",true);
				}else {
					$("#iceAmountStatus").prop("checked",false);
				}
			}else {
				vm.lhpThresholdSetting = {
					equimentId:"",
					equimentName:"",
					levelId:""
				}
				var node = ztree.getNodeByParam("id",treeNode.parentId );
				vm.lhpThresholdSetting.equimentId=node.id;
				vm.lhpThresholdSetting.equimentName=node.name;
				var aaa=treeNode.id;
				vm.lhpThresholdSetting.levelId=aaa.split("-")[1];
				$("#visibilityStatus").prop("checked",false);
				$("#roadSurfaceTempStatus").prop("checked",false);
				$("#frictionStatus").prop("checked",false);
				$("#waterFilmHeightStatus").prop("checked",false);
				$("#roadConditionStatus").prop("checked",false);
				$("#windSpeedStatus").prop("checked",false);
				$("#icePercentageStatus").prop("checked",false);
				$("#relativeHumidityStatus").prop("checked",false);
				$("#airTempStatus").prop("checked",false);
				$("#precipitationIntensityStatus").prop("checked",false);
				$("#precipitationStatus").prop("checked",false);
				$("#waterAmountStatus").prop("checked",false);
				$("#snowAmountStatus").prop("checked",false);
				$("#iceAmountStatus").prop("checked",false);
			}
			form1.render('checkbox');//全局刷新
		});

	}
	return;

}
var ztree;

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		lhpThresholdSetting: {
			equipmentId:"",
			equimentName:"",
			levelId:""
		}
	},
	methods: {
		getEquipment: function(){
			//加载设备树
			$.get(baseURL + "baseinfo/lhpequipment/equipmentTree", function(r){
				ztree = $.fn.zTree.init($("#equipmentTree"), setting, r);
				var node = ztree.getNodeByParam("id", vm.lhpThresholdSetting.equipmentId);

				if(node != null){
					ztree.selectNode(node);
					//vm.lhpEquipment.pointName = node.name;
				}
			})
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			if(vm.lhpThresholdSetting.levelId == ""){
				return;
			}
			let info = JSON.stringify(vm.lhpThresholdSetting).replace(/>/g,"gt").replace(/</g,"lt");
			$('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.lhpThresholdSetting.id == null ? "baseinfo/lhpthresholdsetting/save" : "baseinfo/lhpthresholdsetting/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    //contentType: "application/json",
                    data: {data:info},
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		getInfo: function(id){
			$.get(baseURL + "baseinfo/lhpthresholdsetting/info/"+id, function(r){
                vm.lhpThresholdSetting = r.lhpThresholdSetting;
            });
		},
		reload: function (event) {

		}
	}
});